package com.controller;

import com.entity.Diagnose;
import com.entity.Doctor;
import com.entity.Drug;
import com.entity.RegisterDoc;
import com.service.DoctorService;
import com.service.PatientService;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;
import java.lang.reflect.Array;
import java.util.Date;
import java.util.Vector;

@WebServlet(name = "DoctorServlet", value = "/doctorServlet")
public class DoctorServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        HttpSession session = request.getSession(false);

        if(session == null){
            request.getRequestDispatcher("login.jsp").forward(request,response);
        }

        String skip = request.getParameter("skip");

        /**通过账号获取医生的个人信息**/
        String workId = (String) session.getAttribute("daccount");
        DoctorService doctorService = new DoctorService();
        Doctor doctor = doctorService.findThisDoctor(workId);

        PatientService patientService = new PatientService();

        request.setAttribute("doctorInfo",doctor);


        int state;//决定挂号状态
        switch (skip){
            case "mainPage":         //跳转到医生主页 有当前未处理挂号和已处理挂号按钮
                request.getRequestDispatcher("doctor.jsp").forward(request,response);
                break;

            case "yesDeal":             //跳转到已处理的挂号页面
                state = 1;
                Vector<RegisterDoc> yesRegisterDocs = doctorService.thisDoctorRegisters(doctor.getWorkId(),state);
                request.setAttribute("yesRegisters",yesRegisterDocs);
                request.getRequestDispatcher("yesDeal.jsp").forward(request,response);
                break;

            case "deal":                //跳转到处理页面
                String id = request.getParameter("addId");
                System.out.println("servlet:"+id+" ");
                request.setAttribute("addId",id);
                request.getRequestDispatcher("diagnose.jsp").forward(request,response);


            case "noDeal":              //跳转到未处理的挂号页面 有开药方和下诊断按钮
                state = 0;
                Vector<RegisterDoc> noRegisterDocs = doctorService.thisDoctorRegisters(doctor.getWorkId(),state);
                if(noRegisterDocs==null)
                    System.out.printf("111111");
                request.setAttribute("noRegisters",noRegisterDocs);
                request.getRequestDispatcher("noDeal.jsp").forward(request,response);
                break;

            case "addDeal":             //添加诊断
                String addId = request.getParameter("registerId");

//                RegisterDoc dealRegisterDoc = (RegisterDoc) request.getAttribute("dealRegisterDoc");//从前端获取账户信息

                RegisterDoc dealRegisterDoc = patientService.findRegisterDocById(addId);
                if(dealRegisterDoc==null)
                    System.out.printf("servlet is  null");
                else
                    System.out.printf("is not null");
                String dealPatientAccount = dealRegisterDoc.getAccount();

                String ill = request.getParameter("ill");//获取病情
                Doctor addDoctor = doctorService.findThisDoctor(dealRegisterDoc.getWorkId());
                Date addDate = new Date();
                Diagnose addDiagnose = new Diagnose(ill,dealPatientAccount,addDoctor.getWorkId(),addDate,Integer.parseInt(addId));
                doctorService.addThisDiagnose(addDiagnose);
                doctorService.changeState(addDiagnose.getRegisterDocId());//改变挂号状态
                response.sendRedirect("doctorServlet?skip=noDeal");
                break;

            case"addDrag":              //添加药品
//                String PatientAccount = (String) request.getAttribute("patientAccount");//从前端获取账户信息
//                String doctorWorkId2=(String) request.getAttribute("doctorWorkId");
//                System.out.println("111");
//                System.out.println(doctorWorkId2);
////                Array datas=request.getParameter("tableDatas");
//                String tableDatas []=request.getParameterValues("tableDatas");
//                int i=tableDatas.length;
//                System.out.println(i);
////                for (int j=0;j<i;j++){
////                    String name=tableDatas[j];
////                }
//
//                String firstname = request.getParameter("item.firstName");
//                System.out.println(firstname);
//                String lastName = request.getParameter("item.lastName");
//                System.out.println(lastName);
//                String dragStyle = request.getParameter("index");
//                int dragNum = Integer.parseInt(request.getParameter("index"));
//
//                Drug addDrug = new Drug(dragStyle,dragNum,PatientAccount,doctorWorkId2);
//                doctorService.addThisDrug(addDrug);
                response.sendRedirect("diagnose.jsp");
                break;

            case "addDrugFile":
                String registerId =(String) request.getParameter("registerId");
                RegisterDoc registerDoc=patientService.findRegisterDocById(registerId);
                String patientAccount=registerDoc.getAccount();
                String doctorWorkId=registerDoc.getWorkId();
                request.setAttribute("registerId",registerId);
                request.setAttribute("patientAccount",patientAccount);
                request.setAttribute("doctorWorkId",doctorWorkId);
                request.getRequestDispatcher("recipe.jsp").forward(request,response);
                break;

            case"alDrag":               //跳转到已经开的药品的页面
                Vector<Drug> allDrugs = doctorService.thisDoctorDrugs(workId);
                request.setAttribute("allDrugs",allDrugs);
                request.getRequestDispatcher("alDrugs.jsp").forward(request,response);
                break;

            case "alDeal":              //跳转到已经开的诊断页面
                Vector<Diagnose> allDiagnoses = doctorService.thisDoctorDiagnoses(workId);
                request.setAttribute("allDiagnoses",allDiagnoses);
                request.getRequestDispatcher("alDiagnose.jsp").forward(request,response);
                break;

            case "exit":                //退出登录
                request.getRequestDispatcher("login.jsp").forward(request,response);
                break;
        }

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
